const InfoModel = require('../do/models/info'),
    sequelize = require('sequelize'),
    {appId,secret} =require('../config/config');
const HTTP = require('../lib/http');    
class InfoService extends HTTP {
    async getInfoAll() {
        return await InfoModel.findAll({
            'order': [['createdAt', 'DESC']]
        });
    }
    async getInfo(tid, offset, limit) {
        if (!tid) {
            return await InfoModel.findAll({
                limit: limit,
                offset: offset,
                'order': [['createdAt', 'DESC']]
            }
            );
        }
        return await InfoModel.findOne({
            where: {
                appid: tid
            }
        }
        );
    }
    async getInfoCount() {
        return await InfoModel.findAll({
            attributes: [[sequelize.fn('COUNT', sequelize.col('id')), 'count']]
        })
    }
    async createInfo(data) {
        return await InfoModel.create(data);
    }
    async destroyInfo(id) {
        return await InfoModel.destroy({
            where: {
                id
            }
        })

    }
    appLogin(code){
        return new Promise((resolve) => {
            super.axiosGet({
                url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${secret}&js_code=${code}&grant_type=authorization_code`,
                success(data) {
                    resolve(data)
                }, error(error) {
                   console.log(error);
                }
            })
        })
    }
}
module.exports = new InfoService();